clear all
set more off
cap log close

do "H:\Lavecchia_7086\to-transfer-jan-2022\RESTAT_Replication_Programs\0_Set_Directories.do"


****************************************************************************
* This do file calculate mtr's of individuals
* 
****************************************************************************

cap log using "$dir_log\2g_Calculate_TaxRates.log", replace


****************************************************************************
***** A: Calculate MTR actual (\tau_t)**********
****************************************************************************
clear
foreach years in 8299 0016 {
clear
use "$dir_data\capital_income_final_`years'.dta"

keep lin__i year  txi__i clkgli
sort lin__i year

* need provincial data
merge lin__i year using "$dir_data\taxprovince.dta"
keep if _merge!=2
rename lin__i id 
drop _merge

* correct provincial codes
gen province=.
replace province=txpcoi+1 if txpcoi<=11
replace province=13 if txpcoi==14
replace province=11 if txpcoi==14 & year<=1998 
replace province=6 if txpcoi==. | txpcoi==12 | txpcoi==13 | txpcoi>=93 /* everybod missing prov code assigned to ontario */
drop txpcoi

* prep income variables
gen  intinc=txi__i

* corrections for 1982-1987:
replace intinc=intinc+1000+3560 if year==1982
replace intinc=intinc+1000+3770 if year==1983
replace intinc=intinc+1000+3960 if year==1984
replace intinc=intinc+1000+4140 if year==1985
replace intinc=intinc+1000+4180 if year==1986
replace intinc=intinc+1000+4220 if year==1987

* round to match merge data
replace intinc=round(intinc, 1000)
replace intinc=299000 if intinc>=299000

* merge with calculated MTRs
sort year province intinc
merge year province intinc using "$dir_data/MTR_formerge.dta"
keep if _merge==1 | _merge==3
drop _merge

rename id lin__i
if `years'== 8299 {
* Add exemption data and correct MTRs to account for exemptions
merge 1:1 lin__i year using "$dir_data\exemption_left.dta"
keep if _merge!=2
drop _merge
replace mtr_fed=0 if left_exemption2>clkgli & left_exemption2!=.
replace mtr_prov=0 if left_exemption2>clkgli & left_exemption2!=.
}

gen mtr=mtr_fed+mtr_prov
replace mtr=0 if intinc<=0
* sort and save
keep lin__i year  txi__i mtr
sort lin__i year
save "$dir_data/data_`years'_with_mtr.dta", replace
}


****************************************************************************
***** B: Calculate MTR last year (\tau_{t-1}) **********
****************************************************************************
clear
use "$dir_data/data_8299_with_mtr.dta"
keep if year==1999
replace year=year+1
rename mtr mtr_prev
sort lin__i year
save "$dir_data/data_2000_prev.dta", replace

foreach years in 8299 0016 {
clear
use "$dir_data/data_`years'_with_mtr.dta"
replace year=year+1
rename mtr mtr_prev
sort lin__i year
merge  lin__i year using "$dir_data/data_`years'_with_mtr.dta"
keep if _merge!=1
drop _merge
if `years'==0016{
sort lin__i year
merge  lin__i year using "$dir_data/data_2000_prev.dta", update
keep if _merge!=2
drop _merge
}
sort lin__i year
save "$dir_data/data_`years'_with_mtr.dta", replace
}




****************************************************************************
***** Calculate MTR first dollar **********
****************************************************************************

clear
foreach years in 8299 0016 {
clear
use "$dir_data\capital_income_final_`years'.dta"

keep lin__i year  taxinc_cg xdiv_i invi_i  
*recall: gen agi_cg=tirc_i-clkgli-xdiv_i-invi_i
* gen taxinc_cg=txi__i+ggex_i+klpyc-clkgli 

* emrge with losses data 
sort lin__i year
merge lin__i year using "$dir_data\losses_all.dta"
keep if _merge!=2
drop _merge

replace taxinc_cg=taxinc_cg -xdiv_i-invi_i+losses
keep lin__i year taxinc_cg

* need provincial data
sort lin__i year
merge lin__i year using "$dir_data\taxprovince.dta"
keep if _merge!=2
drop _merge
rename lin__i id 

* correct provincial codes
gen province=.
replace province=txpcoi+1 if txpcoi<=11
replace province=13 if txpcoi==14
replace province=11 if txpcoi==14 & year<=1998 
replace province=6 if txpcoi==. | txpcoi==12 | txpcoi==13 | txpcoi>=93 /* everybod missing prov code assigned to ontario */
drop txpcoi

* prep income variables
gen  intinc= taxinc_cg

* corrections for 1982-1987:
replace intinc=intinc+1000+3560 if year==1982
replace intinc=intinc+1000+3770 if year==1983
replace intinc=intinc+1000+3960 if year==1984
replace intinc=intinc+1000+4140 if year==1985
replace intinc=intinc+1000+4180 if year==1986
replace intinc=intinc+1000+4220 if year==1987

* round to match merge data
replace intinc=round(intinc, 1000)
replace intinc=299000 if intinc>=299000

* merge
sort year province intinc
merge year province intinc using "$dir_data/MTR_formerge.dta"
keep if _merge==1 | _merge==3
drop _merge
rename mtr_fed mtr_fed_zero
rename mtr_prov mtr_prov_zero
replace mtr_fed_zero=0 if intinc<=0
replace mtr_prov_zero=0 if intinc<=0
gen mtr_zero=mtr_fed_zero+mtr_prov_zero

rename id lin__i
keep lin__i year province  mtr_zero

sort lin__i year
merge  lin__i year using "$dir_data/data_`years'_with_mtr.dta"
keep if _merge!=2
drop _merge

if `years'== 8299 {
* Add exemption data and correct MTRs to account for exemptions
merge 1:1 lin__i year using "$dir_data\exemption_left.dta"
keep if _merge!=2
drop _merge
replace mtr_zero=0 if left_exemption2>0 & left_exemption2!=.
drop left_exemption1
}

sort  year province
merge year province using "$dir_data/MTR_formerge_max.dta"
keep if _merge!=2
drop _merge

sort lin__i year

save "$dir_data/data_`years'_with_mtr.dta", replace
}





****************************************************************************
***** B: Calculate MTR next year (\tau_{t+1}) **********
****************************************************************************
clear
use "$dir_data/data_0016_with_mtr.dta"
keep if year==2000
keep lin__i year mtr
replace year=year-1
rename mtr mtr_next
sort lin__i year
save "$dir_data/data_1999_next.dta", replace

foreach years in 8299 0016 {
clear
use "$dir_data/data_`years'_with_mtr.dta"
keep lin__i year mtr
replace year=year-1
rename mtr mtr_next
sort lin__i year
if `years'==8299{
sort lin__i year
merge  lin__i year using "$dir_data/data_1999_next.dta", update
keep if _merge!=2
drop _merge
}
sort lin__i year
save "$dir_data/data_`years'_with_mtr_next.dta", replace
}



*******************************************************************************************************************************
*******************************************************************************************************************************

****************************************************************************
***** A: Calculate MTR actual (\tau_t)**********
****************************************************************************
clear
foreach years in 8299 0016 {
clear
use "$dir_data\capital_income_final_`years'.dta"

keep lin__i year  txi__i clkgli
sort lin__i year

* need provincial data
merge lin__i year using "$dir_data\taxprovince.dta"
keep if _merge!=2
rename lin__i id 
drop _merge

* correct provincial codes
gen province=.
replace province=txpcoi+1 if txpcoi<=11
replace province=13 if txpcoi==14
replace province=11 if txpcoi==14 & year<=1998 
replace province=6 if txpcoi==. | txpcoi==12 | txpcoi==13 | txpcoi>=93 /* everybod missing prov code assigned to ontario */
drop txpcoi

* prep income variables
gen  intinc=txi__i

* corrections for 1982-1987:
replace intinc=intinc+1000+3560 if year==1982
replace intinc=intinc+1000+3770 if year==1983
replace intinc=intinc+1000+3960 if year==1984
replace intinc=intinc+1000+4140 if year==1985
replace intinc=intinc+1000+4180 if year==1986
replace intinc=intinc+1000+4220 if year==1987

* round to match merge data
replace intinc=round(intinc, 1000)
replace intinc=299000 if intinc>=299000

* merge with calculated MTRs
sort year province intinc
merge year province intinc using "$dir_data/MTR_formerge.dta"
keep if _merge==1 | _merge==3
drop _merge

rename id lin__i


gen mtr=mtr_fed+mtr_prov
replace mtr=0 if intinc<=0
* sort and save
keep lin__i year  txi__i mtr
sort lin__i year
save "$dir_data/data_`years'_with_mtr_noexemption.dta", replace
}


****************************************************************************
***** B: Calculate MTR last year (\tau_{t-1}) **********
****************************************************************************
clear
use "$dir_data/data_8299_with_mtr_noexemption.dta"
keep if year==1999
replace year=year+1
rename mtr mtr_prev
sort lin__i year
save "$dir_data/data_2000_prev_noexemption.dta", replace

foreach years in 8299 0016 {
clear
use "$dir_data/data_`years'_with_mtr_noexemption.dta"
replace year=year+1
rename mtr mtr_prev
sort lin__i year
merge  lin__i year using "$dir_data/data_`years'_with_mtr_noexemption.dta"
keep if _merge!=1
drop _merge
if `years'==0016{
sort lin__i year
merge  lin__i year using "$dir_data/data_2000_prev_noexemption.dta", update
keep if _merge!=2
drop _merge
}
sort lin__i year
save "$dir_data/data_`years'_with_mtr_noexemption.dta", replace
}

****************************************************************************
***** Calculate MTR first dollar **********
****************************************************************************

clear
foreach years in 8299 0016 {
clear
use "$dir_data\capital_income_final_`years'.dta"

keep lin__i year  taxinc_cg xdiv_i invi_i  
*recall: gen agi_cg=tirc_i-clkgli-xdiv_i-invi_i
* gen taxinc_cg=txi__i+ggex_i+klpyc-clkgli 

* emrge with losses data 
sort lin__i year
merge lin__i year using "$dir_data\losses_all.dta"
keep if _merge!=2
drop _merge

replace taxinc_cg=taxinc_cg -xdiv_i-invi_i+losses
keep lin__i year taxinc_cg

* need provincial data
sort lin__i year
merge lin__i year using "$dir_data\taxprovince.dta"
keep if _merge!=2
drop _merge
rename lin__i id 

* correct provincial codes
gen province=.
replace province=txpcoi+1 if txpcoi<=11
replace province=13 if txpcoi==14
replace province=11 if txpcoi==14 & year<=1998 
replace province=6 if txpcoi==. | txpcoi==12 | txpcoi==13 | txpcoi>=93 /* everybod missing prov code assigned to ontario */
drop txpcoi

* prep income variables
gen  intinc= taxinc_cg

* corrections for 1982-1987:
replace intinc=intinc+1000+3560 if year==1982
replace intinc=intinc+1000+3770 if year==1983
replace intinc=intinc+1000+3960 if year==1984
replace intinc=intinc+1000+4140 if year==1985
replace intinc=intinc+1000+4180 if year==1986
replace intinc=intinc+1000+4220 if year==1987

* round to match merge data
replace intinc=round(intinc, 1000)
replace intinc=299000 if intinc>=299000

* merge
sort year province intinc
merge year province intinc using "$dir_data/MTR_formerge.dta"
keep if _merge==1 | _merge==3
drop _merge
rename mtr_fed mtr_fed_zero
rename mtr_prov mtr_prov_zero
replace mtr_fed_zero=0 if intinc<=0
replace mtr_prov_zero=0 if intinc<=0
gen mtr_zero=mtr_fed_zero+mtr_prov_zero

rename id lin__i
keep lin__i year province  mtr_zero

sort lin__i year
merge  lin__i year using "$dir_data/data_`years'_with_mtr_noexemption.dta"
keep if _merge!=2
drop _merge


sort  year province
merge year province using "$dir_data/MTR_formerge_max.dta"
keep if _merge!=2
drop _merge

sort lin__i year

save "$dir_data/data_`years'_with_mtr_noexemption.dta", replace
}

****************************************************************************
***** B: Calculate MTR next year (\tau_{t+1}) **********
****************************************************************************
clear
use "$dir_data/data_0016_with_mtr_noexemption.dta"
keep if year==2000
keep lin__i year mtr
replace year=year-1
rename mtr mtr_next
sort lin__i year
save "$dir_data/data_1999_next_noexemption.dta", replace

foreach years in 8299 0016 {
clear
use "$dir_data/data_`years'_with_mtr_noexemption.dta"
keep lin__i year mtr
replace year=year-1
rename mtr mtr_next
sort lin__i year
if `years'==8299{
sort lin__i year
merge  lin__i year using "$dir_data/data_1999_next.dta", update
keep if _merge!=2
drop _merge
}
sort lin__i year
save "$dir_data/data_`years'_with_mtr_next_noexemption.dta", replace
}
